<?php
namespace app\admin\controller;
use app\common\controller\Admin;
use app\common\logic\TypeLogic;

class Type extends Admin
{
    
    /**
     * @desc 如果不是ajax，进入首页。如果是ajax就返回数据咯
     * @return mixed|string
     *
     */
    public function index()
    {
       
        $logic = new TypeLogic();
        
        $params = paramForm();
        
//        $params['parentid'] = isset($params['parentid'])?$params['parentid']:0;
        
       
        
        if(\think\Request::instance()->isAjax()){
            $params['paginate'] = 1;
            if (isset($params['title'])){
                $params['title'] = array('like','%'.$params['title'].'%');
            }
            $categoryList = $logic->getList($params);
            success($categoryList);
        }else{
            $params['parentid'] = isset($params['parentid'])?$params['parentid']:0;
            $this->assign('parentid',$params['parentid']);
            $level = $logic->getLevel($params['parentid']);
            $this->assign('level',$level);
            return $this->fetch();
        
        }
        
    }
    
    public function add(){
    
        $rule=[

            'parentid|父级id'=>'require',
        ];
    
        $logic = new TypeLogic();
        $params = paramForm();
        
        $this->validate($params,$rule);
       

        $this->assign('parentid',$params['parentid']);
    
        $level = $logic->getLevel($params['parentid']);
        $this->assign('level',$level);
        

        
        return $this->fetch();
        
    }
    
    
    public function getFoldList(){
        
        $len = paramForm('len')?paramForm('len'):1;
        $logic = new TypeLogic();
        $types = $logic->getFoldList($len);
        success($types);
        
    }

    public function save(){
        
        
        $params = paramForm();
        $modelClass = $this->modelClass;
        $model = new $modelClass();
        $result = $model->allowField(true)->save($params);

        if($result==1){
            $this->success('新增成功','/type/index?parentid='.$params['parentid']);
        }else{
            $this->error('新增失败');
        }
    }

    /**
     * 改 - 表单
     * @param $id
     * @return string
     */
    public function edit($id)
    {
        $logic = new TypeLogic();
        $type = $logic->findModel($id);
        $this->assign('model',$type);
        return $this->fetch();
    }


    /**
     * 改 - 确认修改
     * @param $id
     * @return string
     */
    public function update($id)
    {
        $logic = new TypeLogic();
        $model = $logic->findModel($id);
        $params = paramFromPost();
        
        $rt = $model->allowField(true)->save($params,['id'=>$id]);

        if ($rt) {
            $this->success("更新成功！", '/type/index?parentid='.$model->parentid);
        } else {
            $this->error("更新失败,或许没有内容更改!");
        }
    }

    /**
     * 删
     * @param $id
     * @return string
     */
    public function delete($id)
    {
        $logic = new TypeLogic();
        $model = $logic->findModel($id);
        $call = '/type/index?parentid='.$model->parentid;
        $rt = $model->delete();

        if ($rt) {
            $this->success("删除成功！", $call);
        } else {
            $this->error("删除失败！");
        }

    }
    
    public function refresh(){
    
    }


}